package com.cn.mmd.realm;


import com.cn.mmd.model.*;
import com.cn.mmd.service.*;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
 * Created by Administrator on 2016/8/9 0009.
 */
public class SecurityRealm extends AuthorizingRealm {

    @Autowired
    private ShiroUserRoleService shiroUserRoleService;

    @Autowired
    private ShiroRoleService shiroRoleService;

    @Autowired
    private ShiroPermissionService shiroPermissionService;

    @Autowired
    private TeacherDataLService teacherDataLService;

    @Autowired
    private StudentAppService studentAppService;

    /**
     * 权限验证
     * @param principalCollection
     * @return
     */
    @Override
    protected synchronized AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
       String loginName = (String) principalCollection.fromRealm(getName()).iterator().next();
        List<ShiroUserRole> shiroUserRoleList = shiroUserRoleService.selectByUserId(loginName);
        if (shiroUserRoleList!=null&&shiroUserRoleList.size()>0){
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            ShiroUserRole shiroUserRole ;
            ShrioRole shrioRole;
            List<ShiroPermission> shiroPermissionList;
            for (int i = 0 ; i < shiroUserRoleList.size() ; i ++){
                shiroUserRole = shiroUserRoleList.get(i);
                shrioRole =shiroRoleService.selectByPrimaryKey(shiroUserRole.getRoleId());
                shiroPermissionList = shiroPermissionService.selectByRoleId(shiroUserRole.getRoleId());
                if (shrioRole!=null){
                    simpleAuthorizationInfo.addRole(shrioRole.getRoleName());
                }
                if (shiroPermissionList!=null&&shiroPermissionList.size()>0){
                    for (int j = 0 ; j < shiroPermissionList.size() ; j ++){
                        System.out.println(shiroPermissionList.get(j).getPermissionName());
                        simpleAuthorizationInfo.addStringPermission(shiroPermissionList.get(j).getPermissionName());
                    }
                }
            }
            return simpleAuthorizationInfo;
        }
        return null;
    }

    /**
     * 登录验证
     * @param authenticationToken
     * @return
     * @throws org.apache.shiro.authc.AuthenticationException
     */
    @Override
    protected synchronized AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;
        TeacherData teacherData =teacherDataLService.selectByPrimaryKey(token.getUsername());
        StudentApp studentApp = studentAppService.selectByPrimaryKey(token.getUsername());
        System.out.println(teacherData + ","+studentApp+","+token.getUsername());
        if (teacherData!=null){
            String username = teacherData.getTeacherid();
            String password = teacherData.getTeacherpassword();
            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, password, getName());
            return authenticationInfo;
        }else if (studentApp!=null){
            String username = studentApp.getStudentid();
            String password = studentApp.getStudentpassword();
            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, password, getName());
            return authenticationInfo;
        }
        return null;
    }
}
